package org.ruanjj.multithread.JUC;

import java.util.Random;
import java.util.concurrent.*;

public class CompletableFutureDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        /*CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(()->{
            System.out.println(Thread.currentThread().getName()+"::completableFuture1::");
        });
        System.out.println(completableFuture.get());*/

        try(ExecutorService threadPool = Executors.newFixedThreadPool(3)){
            CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(()->{
                System.out.println(Thread.currentThread().getName()+"::come in***::");
                int result =  new Random().nextInt(0,10);
                try {
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                return result;
            },threadPool).whenComplete((v,e)->{
                if(e == null){
                    System.out.println(Thread.currentThread().getName()+"最后执行得到的结果为"+v);
                }
            }).exceptionally(e->{
                e.printStackTrace();
                System.out.println("异步执行失败");
                return null;
            });

            CompletableFuture<Integer> completableFuture2 = CompletableFuture.supplyAsync(()->{
                System.out.println(Thread.currentThread().getName()+"::come in***::");
                int result =  new Random().nextInt(0,10);
                try {
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                return result;
            },threadPool).whenComplete((v,e)->{
                if(e == null){
                    System.out.println(Thread.currentThread().getName()+"最后执行得到的结果为"+v);
                }
            }).exceptionally(e->{
                e.printStackTrace();
                System.out.println("异步执行失败");
                return null;
            });

            CompletableFuture<Integer> completableFuture3 = CompletableFuture.supplyAsync(()->{
                System.out.println(Thread.currentThread().getName()+"::come in***::");
                int result =  new Random().nextInt(0,10);
                try {
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                return result;
            },threadPool).whenComplete((v,e)->{
                if(e == null){
                    System.out.println(Thread.currentThread().getName()+"最后执行得到的结果为"+v);
                }
            }).exceptionally(e->{
                e.printStackTrace();
                System.out.println("异步执行失败");
                return null;
            });

            System.out.println("主main 完成自己的任务去了");
        }

    }
}
